package com.jiuyin.dianjing.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.widget.CheckedTextView;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.jiuyin.dianjing.R;
import com.jiuyin.dianjing.api.enums.ApiEnum;
import com.jiuyin.dianjing.api.interfaces.ApiResultCallback;
import com.jiuyin.dianjing.api.interfaces.ProgressDialogCallback;
import com.jiuyin.dianjing.api.server.ServerApi;
import com.jiuyin.dianjing.app.HelperApplication;
import com.jiuyin.dianjing.model.CommentModel;
import com.jiuyin.dianjing.model.ThumbModel;
import com.jiuyin.dianjing.ui.activity.base.BaseActivity;
import com.jiuyin.dianjing.ui.activity.community.CommentActivity;
import com.jiuyin.dianjing.ui.activity.community.PostDetailActivity;
import com.jiuyin.dianjing.util.DisplayUtil;
import com.jiuyin.dianjing.util.IMEUtil;
import com.jiuyin.dianjing.util.LogUtil;
import com.jiuyin.dianjing.util.ToastUtil;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;

import static com.jiuyin.dianjing.api.constant.ApiConstant.KEY_TARGETID;
import static com.jiuyin.dianjing.api.constant.ApiConstant.KEY_TOKEN;
import static com.jiuyin.dianjing.api.constant.ApiConstant.KEY_TYPE;

public class CommentAdapter extends BaseRecyclerAdapter<CommentModel> {
    Context mContext;

    public CommentAdapter(Context context, int layoutId) {
        super(layoutId);
        mContext = context;
    }


    @Override
    protected void onBindViewHolder(SmartViewHolder holder, CommentModel model, int position) {
        ImageView ivHead = holder.itemView.findViewById(R.id.iv_head);
        Glide.with(mContext)
                .load(model.header)
                .override(DisplayUtil.dp2px(mContext, 40))
                .into(ivHead);
        holder.text(R.id.tv_name, model.nickname);
        holder.text(R.id.tv_time, model.create_time);
        holder.text(R.id.tv_comment, model.content);
        CheckedTextView tvFavorite = holder.itemView.findViewById(R.id.tv_favorite);

        tvFavorite.setText(model.thumb_num + "");
        tvFavorite.setChecked(model.isThumb == 1);
        tvFavorite.setOnClickListener(v -> {
            ApiResultCallback apiResultCallback = new ApiResultCallback() {
                @Override
                public void onSuccess(JsonObject data) {
                    ThumbModel thumbModel = new Gson().fromJson(data, ThumbModel.class);
                    model.thumb_num = Integer.parseInt(thumbModel.thumb_num);
                    model.isThumb = thumbModel.isThumb;//更新model,用于跳转到CommentActivity显示正确数据
                    tvFavorite.setChecked(thumbModel.isThumb == 1);
                    tvFavorite.setText(thumbModel.thumb_num);
                    tvFavorite.setEnabled(true);
                }

                @Override
                public void onFail(String msg) {
                    ToastUtil.showShort(msg);
                    tvFavorite.setEnabled(true);
                }

                @Override
                public void onError(String msg) {
                    tvFavorite.setEnabled(true);
                    LogUtil.log(msg);
                }
            };
            HashMap<String, Object> map = new HashMap<>();
            map.put(KEY_TOKEN, HelperApplication.getToken());
            map.put(KEY_TARGETID, model.topiccomment_id);//评论或者 帖子主键
            map.put(KEY_TYPE, tvFavorite.isChecked() ? 0 : 1);//0 取消点赞 1 点赞
            JSONObject json = new JSONObject(map);
            tvFavorite.setEnabled(false);
            ServerApi.post(ApiEnum.APP_TOPIC_THUMB.getUrl(), json, null, new ProgressDialogCallback() {
                @Override
                public void showProgressDialog() {
                    if (mContext instanceof BaseActivity) {
                        ((BaseActivity) mContext).showLoading();
                    }
                }

                @Override
                public void dismissProgressDialog() {
                    if (mContext instanceof BaseActivity) {
                        ((BaseActivity) mContext).dismissLoading();
                    }
                }
            }, null, apiResultCallback);
        });
        TextView tvReply = holder.itemView.findViewById(R.id.tv_reply);
        if (model.reply_num > 0) {
            tvReply.setText(model.reply_num + "回复");
            tvReply.setBackgroundResource(R.drawable.bg_reply);
        } else {
            tvReply.setText("回复");
            tvReply.setBackgroundResource(0);
        }

        holder.itemView.setOnClickListener(v -> {
            IMEUtil.hideInputMethod((Activity) mContext);
            Intent intent = new Intent(mContext, CommentActivity.class);
            intent.putExtra(CommentActivity.MODEL, model);
            ((Activity) mContext).startActivityForResult(intent, PostDetailActivity.RC_FROM_COMMENT_ACTIVITY);
        });
    }
}
